package com.xingdata.jjxc.utils;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.TimeZone;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;

import com.xingdata.jjxc.enty.ColumnType;
import com.xingdata.jjxc.enty.ITableEntity;
import com.xingdata.jjxc.utils.database.Column;

/**
 * 
 * 
 * 项目名称：JJXC_VPN 类名称：StringUtil 类描述： 字符串工具类 创建人：JiangMin 创建时间：2015-5-12
 * 下午5:57:33 修改备注：
 * 
 * @version
 * 
 */
public class StringUtil {
	/**
	 * 日期转换 yyyy-MM-dd
	 * 
	 * @param date
	 * @return
	 */
	public static String ToStringData(Date date) {
		String strFormat = "yyyy-MM-dd";
		SimpleDateFormat dd = new SimpleDateFormat(strFormat);
		return dd.format(date).toString();
	}

	/**
	 * 时间转换 hh:mm:ss
	 * 
	 * @param date
	 * @return
	 */
	public static String ToStringTime(Date date) {
		String strFormat = "hh:mm:ss";
		SimpleDateFormat dd = new SimpleDateFormat(strFormat);
		return dd.format(date).toString();
	}

	/**
	 * 复制两个byte
	 * 
	 * @param data1
	 * @param data2
	 * @return
	 */
	public static byte[] bytecopy(byte[] data1, byte[] data2) {
		byte[] data3 = new byte[data1.length + data2.length];
		System.arraycopy(data1, 0, data3, 0, data1.length);
		System.arraycopy(data2, 0, data3, data1.length, data2.length);
		return data3;
	}

	/**
	 * byte 转hex
	 * 
	 * @param b
	 * @return
	 */
	public static String printHexString(byte[] b) {
		String a = "";
		for (int i = 0; i < b.length; i++) {
			String hex = Integer.toHexString(b[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			a = a + hex;
		}
		Log.i("printHexString", "***********************" + a
				+ "******************************");
		return a;
	}

	/**
	 * ArrayList转换为String字符串
	 * 
	 * @param entities
	 * @param table
	 * @return
	 */
	public static String ArrayListToString(ITableEntity table) {
		ArrayList<String> arrayList = new ArrayList<String>();
		StringBuffer strModel = new StringBuffer();
		for (Column column : table.GetColumns()) {
			String strMethodName = "get" + column.getColumnName();
			if (strMethodName.equals("getNav_amp_current_limits_timeRage")) {
				System.out.println("getNav_amp_current_limits_timeRage");
			}
			Method objMethod;
//			try {
//				objMethod = table.getClass().getDeclaredMethod(strMethodName,
//						null);
//				Object result = objMethod.invoke(table, null);
//				if (strModel.length() != 0) {
//					strModel.append(",");
//				}
//				if (result != null && (result + "").length() != 0) {
//					switch (column.getColumnType()) {
//					case ColumnType.String:
//						strModel.append(result);
//						break;
//					default:
//						break;
//					}
//				} else {
//					strModel.append("\0");
//				}
//			} catch (NoSuchMethodException e) {
//				e.printStackTrace();
//			} catch (IllegalArgumentException e) {
//				e.printStackTrace();
//			} catch (IllegalAccessException e) {
//				e.printStackTrace();
//			} catch (InvocationTargetException e) {
//				e.printStackTrace();
//			}
		}
		strModel.append(";\r\n");
		return strModel.toString();
	}

	public static String getWeeK() {
		final Calendar c = Calendar.getInstance();
		c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
		String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
		if ("1".equals(mWay)) {
			mWay = "天";
		} else if ("2".equals(mWay)) {
			mWay = "一";
		} else if ("3".equals(mWay)) {
			mWay = "二";
		} else if ("4".equals(mWay)) {
			mWay = "三";
		} else if ("5".equals(mWay)) {
			mWay = "四";
		} else if ("6".equals(mWay)) {
			mWay = "五";
		} else if ("7".equals(mWay)) {
			mWay = "六";
		}
		return "星期" + mWay;
	}

	/**
	 * 获取当前日期 *年*月*日
	 * 
	 * @return
	 */
	public static String getDay() {
		final Calendar c = Calendar.getInstance();
		c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
		return String.valueOf(c.get(Calendar.YEAR)) + "年"
				+ String.valueOf(c.get(Calendar.MONTH) + 1) + "月"
				+ String.valueOf(c.get(Calendar.DAY_OF_MONTH)) + "日";
	}

	/**
	 * 将Assets下的文件转化为String字符串
	 * 
	 * @param context
	 * @return
	 */
	public static String getAssetsFileToString(Context context, String fileName) {
		InputStream is;
		String text = "";
		try {
			is = context.getAssets().open(fileName);
			if (is != null) {
				int size = is.available();
				byte[] buffer = new byte[size];
				is.read(buffer);
				is.close();
				text = new String(buffer, "utf-8");
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
		return text;
	}

	/**
	 * byte数组复制
	 * 
	 * @param bList
	 * @return
	 */
	public static byte[] ArrayBytesCopy(ArrayList<byte[]> bList) {
		byte[] b = bList.get(0);
		// 从第一位开始
		for (int i = 1; i < bList.size(); i++) {
			b = StringUtil.bytecopy(b, bList.get(i));
		}
		return b;

	}

	/**
	 * 获取屏幕宽度
	 * 
	 * @return
	 */
	public static int getScreenWidthPixels(Context context) {
		DisplayMetrics dm = new DisplayMetrics();
		((Activity) context).getWindowManager().getDefaultDisplay()
				.getMetrics(dm);
		return dm.widthPixels;
	}

	/**
	 * 获取屏幕高度
	 * 
	 * @return
	 */
	public static int getScreenHeightPixels(Context context) {
		DisplayMetrics dm = new DisplayMetrics();
		((Activity) context).getWindowManager().getDefaultDisplay()
				.getMetrics(dm);
		return dm.heightPixels;
	}

	/**
	 * 判断字符串是否为空 false空
	 * 
	 * @param object
	 * @return
	 */
	public static boolean isStringNull(Object object) {

		return (object != null && object.toString().length() != 0);
	}

	public static String getData(Map<String, Object> map, String columnname) {
		if (map.get(columnname) == null) {
			return "";
		} else {
			return String.valueOf(map.get(columnname));
		}
	}

	/**
	 * 格式化小数
	 * 
	 * @param s
	 * @return
	 */
	public static String formatSize(String s) {
		DecimalFormat df = new DecimalFormat("0.00");//
		float num = Float.parseFloat(s) / 1024 / 1024;
		return df.format(num) + "M";
	}
}
